package com.compass.mobile.widget;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Description： 选中效果
 * Date:2024/5/17 11:47
 */
public class SelectedItemDecoration extends RecyclerView.ItemDecoration {
    private Paint paint;
    private int selectedPosition = -1;

    public SelectedItemDecoration(int color) {
        paint = new Paint();
        paint.setColor(color);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5); // 边框宽度
    }

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (selectedPosition < 0 || selectedPosition >= state.getItemCount())
            return;

        View selected = parent.getLayoutManager().findViewByPosition(selectedPosition);
        if (selected != null) {
            float left = selected.getLeft();
            float right = selected.getRight();
            float top = selected.getTop();
            float bottom = selected.getBottom();
            c.drawRect(left, top, right, bottom, paint);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        // 可以根据需要添加偏移量
    }
}
